using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Security.Cryptography;
using PlugNT.Safe;
using PlugNT.Database.Common;
using PlugNT.Database.Common.Simple;
using PlugNT.Cms.Custom;
using PlugNT.Cms.Model;

namespace PlugNT.Cms.DAL
{
    public class FieldSearch
    {

        private static string currTableName = WebConfig.TablePrefix + "field_search";










        public FieldSearchInfo GetFieldSearchInfo(int id)
        {
            FieldSearchInfo model = null;
            IDataReader dr = GetReader(1, "*", " and id=" + id);
            if (dr != null)
            {
                if (dr.Read())
                {
                    model = new FieldSearchInfo();
                    model.id = int.Parse(dr["id"].ToString());
                    model.sys_model_id = int.Parse(dr["sys_model_id"].ToString());
                    model.column_index = dr["column_index"].ToString();
                    model.title = dr["title"].ToString();
                    model.column = dr["column"].ToString();
                    model.type = dr["type"].ToString();
                    model.symbol = dr["symbol"].ToString();
                    model.type_text = dr["type_text"].ToString();
                    model.symbol_text = dr["symbol_text"].ToString();
                    model.default_value = dr["default_value"].ToString();
                    model.category_no_list = dr["category_no_list"].ToString();
                    model.is_sub_category = dr["is_sub_category"].ToString()=="1";

                }
                dr.Close();

            }
            return model;
        }










        public bool Add(FieldSearchInfo model)
        {


            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into " + currTableName + "(");
            strSql.Append("[sys_model_id],column_index,[title],[column],[type],[symbol],[type_text],[symbol_text],[default_value],category_no_list,is_sub_category");
            strSql.Append(")");
            strSql.Append(" values (");
            strSql.Append(" " + model.sys_model_id + ",");
            strSql.Append("'" + model.sys_model_id.ToString() + PlugNT.Safe.StringHelper.SqlFilter(model.column) + "',");
            strSql.Append("'" + PlugNT.Safe.StringHelper.SqlFilter(model.title) + "',");
            strSql.Append("'" + PlugNT.Safe.StringHelper.SqlFilter(model.column) + "',");
            strSql.Append("'" + PlugNT.Safe.StringHelper.SqlFilter(model.type) + "',");
            strSql.Append("'" + PlugNT.Safe.StringHelper.SqlFilter(model.symbol) + "',");
            strSql.Append("'" + PlugNT.Safe.StringHelper.SqlFilter(model.type_text) + "',");
            strSql.Append("'" + PlugNT.Safe.StringHelper.SqlFilter(model.symbol_text) + "',");
            strSql.Append("'" + PlugNT.Safe.StringHelper.SqlFilter(model.default_value) + "'");
            strSql.Append("'" + PlugNT.Safe.StringHelper.SqlFilter(model.category_no_list) + "',");
            strSql.Append(" " + (model.is_sub_category ? "1" : "0"));

            strSql.Append(")");


            bool flag = false;
            try
            {
                flag = DbHelper.ExecuteSql(strSql.ToString()) > 0;
            }
            catch(Exception ex) 
            {
                SysManager.LogErrorWriteln(strSql.ToString() + "\n" + ex.Message + "\n" + ex.StackTrace);
            }
            return flag;
        }






        public bool Update(FieldSearchInfo model)
        {
            StringBuilder strSql = new StringBuilder();

            strSql.Append("update " + currTableName + " set ");
            strSql.Append("[column_index]='" + model.sys_model_id.ToString() + PlugNT.Safe.StringHelper.SqlFilter(model.column) + "',");
            strSql.Append("[title]='" + PlugNT.Safe.StringHelper.SqlFilter(model.title) + "',");
            strSql.Append("[column]='" + PlugNT.Safe.StringHelper.SqlFilter(model.column) + "',");
            strSql.Append("[type]='" + PlugNT.Safe.StringHelper.SqlFilter(model.type) + "',");
            strSql.Append("[symbol]='" + PlugNT.Safe.StringHelper.SqlFilter(model.symbol) + "',");
            strSql.Append("[type_text]='" + PlugNT.Safe.StringHelper.SqlFilter(model.type_text) + "',");
            strSql.Append("[symbol_text]='" + PlugNT.Safe.StringHelper.SqlFilter(model.symbol_text) + "',");
            strSql.Append("[default_value]='" + PlugNT.Safe.StringHelper.SqlFilter(model.default_value) + "',");
            strSql.Append("[category_no_list]='" + PlugNT.Safe.StringHelper.SqlFilter(model.category_no_list) + "',");
            strSql.Append("[is_sub_category]=" + (model.is_sub_category ? "1" : "0"));

            strSql.Append(" where [id]=" + model.id.ToString());

            return (DbHelper.ExecuteSql(strSql.ToString()) > 0) ? true : false;
        }


        internal static bool Update(int sysModelId, string colName, string categoryNoList, bool isSubCategory)
        {
            StringBuilder strSql = new StringBuilder();

            strSql.Append("update " + currTableName + " set ");
            strSql.Append("[category_no_list]='" + PlugNT.Safe.StringHelper.SqlFilter(categoryNoList) + "',");
            strSql.Append("[is_sub_category]=" + (isSubCategory ? "1" : "0"));
            strSql.Append(" where sys_model_id=" + sysModelId + " and [column]='" + PlugNT.Safe.StringHelper.SqlFilter(colName) + "'");

            return (DbHelper.ExecuteSql(strSql.ToString()) > 0) ? true : false;
        }






        public bool Delete(int id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from " + currTableName);
            strSql.Append(" where id=" + id.ToString());
            return (DbHelper.ExecuteSql(strSql.ToString()) > 0) ? true : false;
        }













        private IDataReader GetReader(int topCount, string colNames, string andWhere)
        {
            string sql = "select " + (topCount == -1 ? "" : "top " + topCount) + " " + colNames + " from " + currTableName + " where 1=1 " + andWhere;
            return DbHelper.ExecuteReader(sql);
        }






        public DataTable GetList(int sysModelId, string andWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * from  [" + currTableName + "] where sys_model_id= " + sysModelId + andWhere);
            return DbHelper.GetDataTable(strSql.ToString());
        }


 








        public int GetCount(int sysModelId, string andWhere)
        {
            string sql = "select count(1) from [" + currTableName + "] where  and sys_model_id= " + sysModelId + andWhere;
            return (int)DbHelper.GetSingle(sql);
        }







        public bool Exists(int sysModelId, string colName)
        {
            if (colName != "")
            {
                if (WebConfig.IsAccess)
                {
                    StringBuilder strSql = new StringBuilder();
                    strSql.Append("select top 1 * from  [" + Article.currTableName + sysModelId + "] where 1=0");
                    DataTable dt = DbHelper.GetDataTable(strSql.ToString());
                    return dt.Columns.Contains(colName);
                }
                else
                {
                    return DbHelper.ExistsColumn(Article.currTableName + sysModelId, PlugNT.Safe.StringHelper.SqlFilter(colName));
                }
            }
            else
            {
                return false;
            }
        }

    }
}

